<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>获取随机颜色</title>
</head>
<body>
    <script type="text/javascript">

        // 1. 获取min到max之间的整数(1~100)
        /*
        var max = 100;
        var min = 1;
        var intNum = Math.floor(Math.random() * (max-min) + min);
        console.log(intNum);
        */

        // 使用函数实现
        /*
        function random(max,min){
            return Math.floor(Math.random() * (max-min) + min);
        }
        */
        // 2. 获取随机颜色 rgb(0~255,0~255,0~255);
        function randomColor(){
            var r = Math.floor(Math.random() * 256), g = Math.floor(Math.random() * 256),b = Math.floor(Math.random() * 256);
            // 模版字符串
            var result = `rgb(${r},${g},${b})`;
            return result;
        }
        var rc = randomColor();
        console.log(rc);
        document.body.style.backgroundColor = rc;


        // 3. 随机验证码 四位 数字 + 字母(大写)
        function createCode(){
            // 设置默认的空的字符串
            var code = '';
            // 设置长度
            var codelength = 4;
            var randomCode = [0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
            for(var i = 0; i < codelength;i++){
                // 设置随机范围 0~36
                var index = Math.ceil(Math.random()*35);
                code += randomCode[index];

            }
            return code;
        }
        var rndcode = createCode();
        console.log(rndcode);
        document.write(`<h1>${rndcode}</h1>`);

    </script>

</body>
</html>